package com.pickgliss.bitmapEngin
{
	import com.pickgliss.bitmapEngin.cache.CacheClip;
	import com.pickgliss.bitmapEngin.cache.CacheFrame;
	import com.pickgliss.bitmapEngin.ds.CacheClipList;
	import com.pickgliss.ui.core.Disposeable;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.PixelSnapping;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class BitmapRenderLayer extends Bitmap implements Disposeable
	{
		public function BitmapRenderLayer($id:String,$width:int,$height:int,$renderCopy:Boolean = true,$renderSmoothing:Boolean = true)
		{
			super(new BitmapData($width,$height,true,0xff0000),PixelSnapping.AUTO,false);
			_layerRect = new Rectangle(0,0,$width,$height);
			_renderCopy = $renderCopy;
			_renderSmoothing = $renderSmoothing;
			_renderList = new Vector.<CacheClipList>();
			id = $id;
		}
		
		protected var _hasContentToErase:Boolean = false;
		protected var _layerRect:Rectangle;
		protected var _renderColorTransfrom:ColorTransform = null;
		protected var _renderCopy:Boolean;
		protected var _renderList:Vector.<CacheClipList>;
		protected var _renderMatrix:Matrix	 = new Matrix();
		protected var _renderPoint:Point = new Point();
		protected var _renderRec:Rectangle = new Rectangle();
		protected var _renderSmoothing:Boolean;
		public var id:String;
		
		public function addRenderList(list:CacheClipList):void
		{
			_renderList.push(list);
		}
		
		public function dispose():void
		{
			bitmapData.dispose();
			_renderList = null;
			if(parent)parent.removeChild(this);
		}
		
		public function removeRenderList(list:CacheClipList):void
		{
			_renderList = _renderList.splice(_renderList.indexOf(list),1);
		}
		
		public function render():void
		{
			if(stage == null)return;
			bitmapData.lock();
			if (_hasContentToErase)
			{
				bitmapData.fillRect (_layerRect, 0x00000000);
				_hasContentToErase = false ;
			}
			var renderListsLength:int = _renderList.length;
			for (var j:int = 0 ; j<renderListsLength ; ++j)
			{
				var cacheList:CacheClipList = _renderList[j];
				var clip:CacheClip;
				if (cacheList.head)
				{
					clip = cacheList.head;
				}
				else
				{
					continue;
				}
				
				while (clip)
				{					
					if (clip.visible)
					{
						var frame:CacheFrame 	= clip.frame;
						_renderColorTransfrom = clip.colorTransform;
						
						if (_renderCopy && !_renderColorTransfrom && clip.rotation == 0)
						{
							_renderRec.width = frame.width;
							_renderRec.height = frame.height;
							_renderPoint.x = clip.x - frame.registerX;
							_renderPoint.y = clip.y - frame.registerY;
							bitmapData.copyPixels(frame.bitmapData,_renderRec,_renderPoint,null,null,true);
						}
						else
						{
							_renderMatrix.identity();
							_renderMatrix.translate(- frame.registerX, - frame.registerY);
							_renderMatrix.scale (clip.scaleX, clip.scaleY);
							if ( clip.rotation != 0 )_renderMatrix.rotate(clip.rotation / 180 * 3.14159); 
							_renderMatrix.translate(clip.x, clip.y);
							bitmapData.draw (frame.bitmapData, _renderMatrix, _renderColorTransfrom, clip.blendMode, null, _renderSmoothing);
						}
						_hasContentToErase = true;
					}
					if (clip.isPlaying)clip.update();
					clip = clip.next;
				}
			}
			bitmapData.unlock();
		}
	}
}